// pages/complaint/complaint.js
var app=getApp();
var main = require('../../utils/main.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList:[],
    httpApiImg: app.globalData.httpApiImg,
    content:'',
    toUserId:'',
    userInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    main.changeInput.apply(this, []);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      toUserId: options.userId
    })
  },
  complaint:function(e){
    if (this.data.content!=''){
      wx.request({
        url: app.globalData.httpApiCar + 'tGyComplaint/save', //仅为示例，并非真实的接口地址
        data: {
          fromUserId: this.data.userInfo.id,
          toUserId: this.data.toUserId,
          picture: this.data.imgList.join(','),
          content: this.data.content
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            wx.navigateBack({
              delta:1
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  delImg:function(e){
    var index=e.currentTarget.dataset.index;
    this.data.imgList.splice(index,1);
    this.setData({
      imgList: this.data.imgList
    })
  },
  uploadImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.upload(tempFilePaths);
      }
    })
  },
  /*上传图片*/
  upload: function (path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: app.globalData.httpApiCar + 'uploadfile/upload1',
        filePath: path[0],
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {},
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          that.data.imgList.push(url);
          that.setData({
            imgList: that.data.imgList
          })
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})